/*
 * Copyright (C) 2001-2016 Food and Agriculture Organization of the
 * United Nations (FAO-UN), United Nations World Food Programme (WFP)
 * and United Nations Environment Programme (UNEP)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 *
 * Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
 * Rome - Italy. email: geonetwork@osgeo.org
 */

package org.fao.geonet.domain;

import org.fao.geonet.entitylistener.MapServerEntityListenerManager;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.persistence.*;

import java.util.Map;

/**
 * An entity representing mapserver used for publishing records as WMS, WFS, WCS.
 *
 * Note: This configuration was previously stored in wEB-INF/geoserver-nodes.xml
 *
 * @author Francois
 */
@Entity
@Table(name = "Mapservers")
@Cacheable
@Access(AccessType.PROPERTY)
@EntityListeners(MapServerEntityListenerManager.class)
@SequenceGenerator(name = MapServer.ID_SEQ_NAME, initialValue = 100, allocationSize = 1)
public class MapServer extends GeonetEntity {
    static final String ID_SEQ_NAME = "mapserver_id_seq";

    private int _id;
    private String _name;
    private String _description;
    private String _configurl;
    private String _wmsurl;
    private String _wfsurl;
    private String _wcsurl;
    private String _stylerurl;
    private String _username;
    private String _password;
    private String _namespaceprefix;
    private String _namespace;
    private char _pushstyleinworkspace = Constants.YN_FALSE;

    /**
     * Get the id of the mapserver. <p> This is autogenerated and when a new mapserver is created
     * the mapserver will be assigned a new value. </p>
     *
     * @return the id of the mapserver.
     */
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = ID_SEQ_NAME)
    @Column(nullable = false)
    public int getId() {
        return _id;
    }

    /**
     * Set the id of the mapserver. <p> If you want to update an existing mapserver then you should
     * set this id to the mapserver you want to update and set the other values to the desired
     * values. </p>
     *
     * @param id the id of the group.
     * @return this mapserver object
     */
    public MapServer setId(int id) {
        this._id = id;
        return this;
    }

    /**
     * Get the basic/default name of the mapserver. This is non-translated and can be used to look
     * up the mapserver like an id can. <p> This is a required property. <p> There is a max length
     * to the name allowed. See the annotation for the length value. </p>
     *
     * @return mapserver name
     */
    @Column(nullable = false, length = 32)
    public String getName() {
        return _name;
    }

    /**
     * Set the basic/default name of the mapserver. This is non-translated and can be used to look
     * up the mapserver like an id can. <p> This is a required property. <p> There is a max length
     * to the name allowed. See the annotation on {@link #getName()} for the length value. </p>
     */
    public MapServer setName(String name) {
        this._name = name;
        return this;
    }

    /**
     * Get a description of the mapserver.
     *
     * @return the description.
     */
    @Column(length = 255)
    public String getDescription() {
        return _description;
    }

    /**
     * Set the mapserver description.
     *
     * @param description the description.
     * @return this mapserver object.
     */
    public MapServer setDescription(String description) {
        this._description = description;
        return this;
    }

    /**
     * Get the REST API configuration URL for the mapserver.
     *
     * @return the REST API URL.
     */
    @Column(nullable = false, length = 255)
    public String getConfigurl() {
        return _configurl;
    }

    /**
     * Set the REST API configuration URL for the mapserver.
     *
     * @param configurl the server URL.
     * @return this mapserver object.
     */
    public MapServer setConfigurl(String configurl) {
        this._configurl = configurl;
        return this;
    }

    /**
     * Get the WMS URL for the mapserver.
     *
     * @return the WMS URL.
     */
    @Column(length = 255)
    public String getWmsurl() {
        return _wmsurl;
    }

    public MapServer setWmsurl(String _wmsurl) {
        this._wmsurl = _wmsurl;
        return this;
    }

    /**
     * Get the WFS URL for the mapserver.
     *
     * @return the WFS URL.
     */
    @Column(length = 255)
    public String getWfsurl() {
        return _wfsurl;
    }

    public MapServer setWfsurl(String _wfsurl) {
        this._wfsurl = _wfsurl;
        return this;
    }

    /**
     * Get the WCS URL for the mapserver.
     *
     * @return the WCS URL.
     */
    @Column(length = 255)
    public String getWcsurl() {
        return _wcsurl;
    }

    public MapServer setWcsurl(String _wcsurl) {
        this._wcsurl = _wcsurl;
        return this;
    }

    /**
     * Get the styler URL for the mapserver.
     *
     * @return the styler URL.
     */
    @Column(length = 255)
    public String getStylerurl() {
        return _stylerurl;
    }

    public MapServer setStylerurl(String _stylerurl) {
        this._stylerurl = _stylerurl;
        return this;
    }

    /**
     * Get the username to use for connecting to the mapserver.
     *
     * @return the username.
     */
    @Column(length = 128)
    public String getUsername() {
        return _username;
    }

    public MapServer setUsername(String _username) {
        this._username = _username;
        return this;
    }

    /**
     * Get the password to use for connecting to the mapserver.
     *
     * @return the password.
     */
    @Column(length = 128)
    public String getPassword() {
        return _password;
    }

    public MapServer setPassword(String _password) {
        this._password = _password;
        return this;
    }

    /**
     * Get the workspace namespace prefix to use for publishing datasets into the mapserver.
     *
     * @return the workspace namespace prefix.
     */
    public String getNamespacePrefix() {
        return _namespaceprefix;
    }

    public MapServer setNamespacePrefix(String _namespaceprefix) {
        this._namespaceprefix = _namespaceprefix;
        return this;
    }

    /**
     * Get the workspace namespace URL to use for publishing datasets into the mapserver.
     *
     * @return the workspace namespace URL.
     */
    public String getNamespace() {
        return _namespace;
    }

    public MapServer setNamespace(String _namespace) {
        this._namespace = _namespace;
        return this;
    }

    /**
     * Check whether the sld style should be pushed in the same workspace as the layer and datastore
     * or in the global styles/ dir
     */

    @Column(name = "pushstyleinworkspace", length = 1)
    protected char getPushStyleInWorkspace_JpaWorkaround() {
        return _pushstyleinworkspace;
    }

    protected void setPushStyleInWorkspace_JpaWorkaround(char _pushStyleInWorkspace) {
        this._pushstyleinworkspace = _pushStyleInWorkspace;
    }

    @Transient
    public boolean pushStyleInWorkspace() {
        return Constants.toBoolean_fromYNChar(getPushStyleInWorkspace_JpaWorkaround());
    }

    public MapServer setPushStyleInWorkspace(boolean _pushStyleInWorkspace) {
        setPushStyleInWorkspace_JpaWorkaround(Constants.toYN_EnabledChar(_pushStyleInWorkspace));
        return this;
    }

    @Override
    public String toString() {
        return "MapServer [_id=" + _id + ", _name=" + _name + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + _id;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        MapServer other = (MapServer) obj;
        if (_id != other._id)
            return false;
        return true;
    }

}
